package com.crm.system.service.impl.csd.customerManager;

import com.crm.common.exception.FastDFSException;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.csd.customerManager.CustomerServiceWorkLog;
import com.crm.model.entity.sys.FileUpload;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.csd.customerManager.CustomerServiceWorkLogQO;
import com.crm.model.vo.csd.customerManager.CustomerServiceWorkLogVO;
import com.crm.service.csd.customerManager.CustomerServiceWorkLogDbService;
import com.crm.system.service.FileUploadService;
import com.crm.system.service.csd.customerManager.CustomerServiceWorkLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author yincuimei
 * @Date 2021/8/20 15:23
 */
@Service
public class CustomerServiceWorkLogServiceImpl implements CustomerServiceWorkLogService {
    @Autowired
    private CustomerServiceWorkLogDbService serviceWorkLogDbService;

    @Autowired
    FileUploadService fileUploadService;

    /**
     * (客服部)工作日志列表--根据客户ID查询工作日志
     * @param customerId --客户ID
     * @author yincuimei
     * @return
     */
    @Override
    public DhPageInfo<CustomerServiceWorkLogVO> selectWorkLogList(String customerId, Integer pageIndex, Integer pageSize, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "updateTm": {
                    order.append("t.update_tm ").append(sortOrder);//操作时间
                }
                break;
                default:
                    break;
            }
        }
        List<CustomerServiceWorkLogVO> workLogVOList = serviceWorkLogDbService.selectWorkLogList(customerId,order.toString());
        PageInfo<CustomerServiceWorkLogVO> pageInfo = new PageInfo<CustomerServiceWorkLogVO>(workLogVOList);
        return new DhPageInfo<CustomerServiceWorkLogVO>(pageInfo, workLogVOList);
    }

    /**
     * 工作日志--查看
     * @param workLogId --日志主键
     * @return
     */
    @Override
    public CustomerServiceWorkLogVO selectWorkLogById(String workLogId) {
        CustomerServiceWorkLogVO workLogVO = null;
        if(!StringUtils.isBlank(workLogId)){
            //1.查询工作日志表(csd_work_log)
            workLogVO = serviceWorkLogDbService.selectWorkLogById(workLogId);
            if(workLogVO != null){
                String uploadId = workLogVO.getUploadId();
                if(!StringUtils.isBlank(uploadId)){//日志附件不为空
                    //2.查询文件表(sys_file_upload)
                   FileUpload fileUpload = fileUploadService.selectFileUploadById(workLogVO.getUploadId());
                   if(fileUpload != null){
                       workLogVO.setAccessUrl(fileUpload.getAccessUrl());//附件访问地址
                       workLogVO.setFileName(fileUpload.getFileName());//文件名
                   }
                }
            }
        }
        return workLogVO;
    }

    /**
     * (客服部)批量新增工作日志
     * @param customerServiceWorkLogList --工作日志集合
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean insertWorkLogBatch(List<CustomerServiceWorkLog> customerServiceWorkLogList) {
        return serviceWorkLogDbService.insertWorkLogBatch(customerServiceWorkLogList);
    }

    /**
     * (客服部)新增工作日志
     * @param customerServiceWorkLog --工作日志实体
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean insertWorkLog(CustomerServiceWorkLog customerServiceWorkLog) {
        return serviceWorkLogDbService.insertWorkLog(customerServiceWorkLog);
    }

    /**
     * 日志实体
     * @author yincuimei
     * @return
     */
    @Override
    public CustomerServiceWorkLog getCustomerServiceWorkLog(String accountId,String customerId,short logType,String logContent,String logRemark){
        CustomerServiceWorkLog customerServiceWorkLog = new CustomerServiceWorkLog();
        customerServiceWorkLog.setWorkLogId(CommonUtils.getGUID());//主键
        customerServiceWorkLog.setLogType(logType);//日志类型
        customerServiceWorkLog.setCreatePeopleId(accountId);//创建人
        customerServiceWorkLog.setCreateTm(new Date());//创建时间
        customerServiceWorkLog.setUpdatePeopleId(accountId);//操作人ID(更新人ID)
        customerServiceWorkLog.setUpdateTm(new Date());//操作时间(更新时间)
        customerServiceWorkLog.setCustomerId(customerId);//客户ID
        customerServiceWorkLog.setLogRemark(logRemark);//备注
        customerServiceWorkLog.setLogContent(logContent);//操作内容
        customerServiceWorkLog.setDeleteStatus((short)StatusCodeEnum.NORMAL_CODE.getState());//删除状态--0:正常; 1:已删除
        return customerServiceWorkLog;
    }

    /**
     * 日志实体
     * @param accountId
     * @param customerId
     * @param logType
     * @param logContent
     * @param logRemark
     * @param contactPersonId
     * @return CustomerServiceWorkLog
     * @author wangpeng
     */
    @Override
    public CustomerServiceWorkLog getCustomerServiceWorkLog(String accountId,String customerId,short logType,String logContent,String logRemark,String contactPersonId){
        CustomerServiceWorkLog customerServiceWorkLog = getCustomerServiceWorkLog(accountId, customerId, logType, logContent, logRemark);
        customerServiceWorkLog.setRelationId(contactPersonId);
        return customerServiceWorkLog;
    }

    /**
     * 工作日志--修改备注、上传附件
     * @param workLogQO     --修改实体
     * @param multipartFile --日志附件
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateWorkLogById(CustomerServiceWorkLogQO workLogQO, MultipartFile multipartFile) throws FastDFSException {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        if(workLogQO != null && !StringUtils.isBlank(workLogQO.getWorkLogId())){
            CustomerServiceWorkLog workLog =  new CustomerServiceWorkLog();
            workLog.setWorkLogId(workLogQO.getWorkLogId());//主键
            if(!StringUtils.isBlank(workLogQO.getLogRemark())){
                workLog.setLogRemark(workLogQO.getLogRemark() + "  " + userAccount.getUserName());//备注
            }
            if(multipartFile != null){//附件不为空
                //上传附件
                FileUpload fileUpload = fileUploadService.uploadFile(multipartFile, false);
                if(fileUpload != null){//上传成功
                    workLog.setUploadId(fileUpload.getUploadId());
                }
            }
            workLog.setUpdatePeopleId(userAccount.getAccountId());
            workLog.setUpdateTm(new Date());
            //修改工作日志
            return serviceWorkLogDbService.updateWorkLogById(workLog);
        }
        return false;
    }
}
